package scooper.cn.sc_base.log;

import android.text.TextUtils;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zhaoboliang on 2017/1/16.
 */

public class SCLog {

    //日志文件等级，外部修改；小于等于0，禁止写入。
    private static int logLevel = 3;

    private static String LOG_PATH = "";

    private static boolean isLogPathInitOk = false;

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd" +
            " HH:mm:ss", Locale.getDefault());

    public static void setLogLevel(int logLevel) {
        SCLog.logLevel = logLevel;
    }

    public static void initLogConfig(LogConfig config) {
        LogProcessClient.getInstance().initLog(config);
    }

    public static void setDefaultLogConfig(LogConfig config) {
        LogProcessClient.getInstance().setDefaultLogConfig(config);
    }

    public static void checkAndDeleteOverdueLog() {
        LogProcessClient.getInstance().checkAndDeleteOverdueLog();
    }


    public static void write2file(String message) {
        SCLog.toFile(message);
    }

    /**
     * String path //文件log路径 不是文件夹路径！！！！！！
     * return false 不成功 true 成功
     * **/
//    public static boolean initLogFileWithFileName(String parentPath,String fileName,int size) {
//
//        boolean isOk = SCFileUtils.createFile(parentPath,fileName);
//
//        if(isOk) {
//
//
//
//            if (logFile.exists() && logFile.isFile()) {
//
//                isLogPathInitOk = true;
//                LOG_PATH = parentPath + File.separator + fileName;
//                //超过4M就覆盖
//                if (logFile.length() >= 1024 * 1024 * size) {
//                    SCFileUtils.overlayContentToFile(logFile.getAbsolutePath(), "");
//                }
//                return true;
//            }
//        }
//        return false;
//    }


    public static void toFile(String msg) {
        writeToLogFile(null, null, msg);
    }

    public static void e(String tag, String format, Object... args) {

        String msg  = String.format(Locale.getDefault(), format, args);
        Log.e(tag, msg);

        if (logLevel >= 1) {
            writeToLogFile("ERROR", tag, msg);
        }
    }


    public static void e(String tag, String msg) {
        Log.e(tag, msg);
        if (logLevel >= 1) {
            writeToLogFile("ERROR", tag, msg);
        }
    }

    public static void w(String tag, String format, Object... args) {

        String msg  = String.format(Locale.getDefault(), format, args);

        Log.w(tag, msg);
        if (logLevel >= 2) {
            writeToLogFile("WARN", tag, msg);
        }
    }

    public static void w(String tag, String msg) {

        Log.w(tag, msg);
        if (logLevel >= 2) {
            writeToLogFile("WARN", tag, msg);
        }
    }

    public static void i(String tag, String format, Object... args) {

        String msg  = String.format(Locale.getDefault(), format, args);

        Log.i(tag, msg);
        if (logLevel >= 3) {
            writeToLogFile("INFO", tag, msg);
        }
    }

    public static void i(String tag, String msg) {
        Log.i(tag, msg);
        if (logLevel >= 3) {
            writeToLogFile("INFO", tag, msg);
        }
    }

    public static void d(String tag, String format, Object... args) {

        String msg  = String.format(Locale.getDefault(), format, args);

        Log.d(tag, msg);
        if (logLevel >= 4) {
            writeToLogFile("DEBUG", tag, msg);
        }
    }

    public static void d(String tag, String msg) {
        Log.d(tag, msg);
        if (logLevel >= 4) {
            writeToLogFile("DEBUG", tag, msg);
        }
    }

    public static void v(String tag, String format, Object... args) {

        String msg  = String.format(Locale.getDefault(), format, args);
        Log.v(tag, msg);
        if (logLevel >= 5) {
            writeToLogFile("VERBOSE", tag, msg);
        }
    }


    public static void v(String tag, String msg) {
        Log.v(tag, msg);
        if (logLevel >= 5) {
            writeToLogFile("VERBOSE", tag, msg);
        }
    }

    private static void writeToLogFile(String level, String tag, String msg) {

//        if (!isLogPathInitOk) {
//            return;
//        }

        String time = format.format(new Date());

        StackTraceElement caller = getCallerStackTraceElement();
        //
        String fileText = generateTag(time, caller, tag);

        String content = fileText + " " + level + ": " + msg + "\r\n";
        LogProcessClient.getInstance().writeToFile(caller.getClassName(), content);
//        SCFileUtils.appendContentToFile(LOG_PATH, fileText + " " + level + ": "
//                + msg + "\n");
    }

    //获取函数调用地方（方法栈的方式获取）
    private static StackTraceElement getCallerStackTraceElement() {
        return Thread.currentThread().getStackTrace()[5];
    }

    private static String generateTag(String time, StackTraceElement caller,
                                      String customTagPrefix) {


        String tag = "%s %s.%s(Line:%d) tag: %s "; // 占位符
        if (TextUtils.isEmpty(customTagPrefix)) {
            tag = "%s %s.%s(Line:%d) message:\n";
        }
        String callerClazzName = caller.getClassName(); // 获取到类名

        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);

        return String.format(Locale.getDefault(), tag, time, callerClazzName, caller
                .getMethodName(), caller.getLineNumber(), customTagPrefix); // 替换
    }

}
